/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.httprequest;

import cn.ac.iie.di.global.GlobalParas;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;

/**
 *
 * @author shipengfeng
 */
public class MyHttpRequest1 implements Runnable {

        Logger logger = Logger.getLogger(MyHttpRequest1.class.getName());

        @Override
        public void run() {
                logger.info("start the httpRequest ");
                try {
                        String[] urlPaths = GlobalParas.httpPostAddr;
                        int urlIdx = 0;
                        JettyClient jc = new JettyClient();
                        while (true) {
                                String msg = null;
                                msg = GlobalParas.send2MyHttpRequestPool.poll(3, TimeUnit.SECONDS);
                                if (msg != null) {
                                        msg = msg.replaceAll("\n", "").replaceAll("\r", "");
                                        long st = System.currentTimeMillis();
                                        int tryTimes = 0;
                                        while (true) {
                                                try {
//                                                        String msg = jo.toJSONString();
                                                        if (urlIdx >= urlPaths.length) {
                                                                urlIdx = 0;
                                                        }
                                                        String feedBack = jc.send(urlPaths[urlIdx], msg).replaceAll("\n", "").replaceAll("\r", "");
                                                        urlIdx++;
                                                        logger.info("FeedBack is " + feedBack + " send time is: " + (System.currentTimeMillis() - st));
                                                        break;
                                                } catch (Exception ex) {
                                                        if (tryTimes < 10) {
                                                                logger.error("send msg err " + ex.getMessage(), ex);
                                                                tryTimes++;
                                                                Thread.sleep(500);
                                                        } else {
                                                                break;
                                                        }
                                                }
                                        }
                                }
                        }
                } catch (Exception e) {
                        logger.error(e, e);
                }

//                if (!GlobalParas.httpRequestPool.isEmpty()) {
//                        GlobalParas.httpRequestPool.poll();
//                        logger.info("kill MyHttpRequest nums is " + GlobalParas.httpRequestPool.size());
//                }
        }
}
